package com.wangjiacai.utils.cos.impl;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.Bucket;
import com.wangjiacai.utils.cos.BucketService;
import com.wangjiacai.utils.cos.CosRepository;

import java.util.List;

public class BucketServiceImpl extends CosRepository implements BucketService {

    public BucketServiceImpl(String secretId, String secretKey, String region) {
        super(secretId, secretKey, region);
    }

    @Override
    public List<Bucket> listBuckets() {
        COSClient cosClient = getCosClient();
        return cosClient.listBuckets();
    }

    @Override
    public Bucket createBucket(String bucketName) {
        Bucket bucket=findBucketByName(bucketName);
        if(bucket!=null) return bucket;
        COSClient cosClient = getCosClient();
        return cosClient.createBucket(bucketName);
    }

    @Override
    public void deleteBucket(String bucketName) {
        COSClient cosClient = getCosClient();
        cosClient.deleteBucket(bucketName);
    }

    @Override
    public Bucket findBucketByName(String bucketName) {
        List<Bucket> list = listBuckets();
        for (Bucket bucket : list) {
            if (bucket.getName().equals(bucketName)) return bucket;
        }
        return null;
    }
}
